<?php


namespace app\adminapi\controller\v1\car;


use app\adminapi\controller\AuthController;
use app\services\car\CarBlacklistServices;
use think\facade\App;

class CarBlacklist extends AuthController
{
    public function __construct(App $app,CarBlacklistServices $services)
    {
        parent::__construct($app);
        return $this->services = $services;
    }

    /**
     * 列表
     * @return \think\Response
     */
    public function getList(){
        $where = $this->request->getMore([
            ['keyword','']
        ]);
        return app('json')->success($this->services->getList($where,true));
    }

    /**
     * 添加
     * @return \think\Response
     * @throws \FormBuilder\Exception\FormBuilderException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function add(){
        $data = $this->request->getMore([
            ['id', 0],
        ]);
        return app('json')->success($this->services->add((int)$data['id']));
    }

    /**
     * 保存
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function save(){
        $data = $this->request->postMore([
            ['id', 0],
            ['phone',''],
            ['card_id',''],
            ['remark','']
        ]);
        if (!$data['phone']&&!$data['card_id']) return app('json')->fail('请输入电话号码或身份证号');
        if($data['phone']) if(!check_phone($data['phone'])) return app('json')->fail('手机号格式不正确');
        if($data['card_id']) if(!check_card($data['card_id'])) return app('json')->fail('身份证号格式不正确');
        $this->services->save((int)$data['id'], $data);
        return app('json')->success('提交成功');
    }

    /**
     * 删除
     * @return \think\Response
     */
    public function delete(){
        $data = $this->request->getMore([
            ['id', 0],
        ]);
        if (!$data['id']) return app('json')->fail('参数错误');
        return app('json')->success($this->services->delete((int)$data['id']));
    }
}
